<%@tag pageEncoding="UTF-8" %>

<%@tag import="com.wondersgroup.framework.widget.library.extjs.form.ExtHidden"%>
<%@tag import="com.wondersgroup.framework.widget.library.extjs.form.ExtField"%>
<%@tag import="com.wondersgroup.framework.widget.taglib.form.FormConstants"%>
<%@tag import="com.wondersgroup.framework.widget.taglib.form.FormTag"%>

<%@tag import="java.util.List"%>
<%@tag import="java.util.Map"%>
<%@tag import="java.util.HashMap"%>
<!-- 属性申明 -->
<%@ attribute name="id" required="true" rtexprvalue="true" description="标签id"%>
<%@ attribute name="property" required="true" rtexprvalue="true"  description="标签Property"%>
<%@ attribute name="value" required="false" rtexprvalue="true"  description="标签初始值"%>
<%@ attribute name="width" required="false"  description="宽度"%> 
<%@ attribute name="label" required="false"  description="标签中文名称"%> 
<%@ attribute name="required" required="false"  description="是否必填"%> 
<%@ attribute name="readOnly" required="false"  description="是否只读"%> 
<%@ attribute name="showcn" required="false"  description="显示中文年月"%> 
<%@ attribute name="listeners" required="false"  description="监听器"%> 
<%@ attribute name="hidden" required="false" description="是否隐藏"%>
 
<!-- 引入第三方类库 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="cw" uri="/widget-tags" %>
 
<%
 
String hid="_"+id+"_HIDDEN";

ExtHidden ext =new ExtHidden(hid);
ext.setProperty(property);

Map js =(Map)request.getAttribute("tag_ym");

if(js==null){
	String ymJs=request.getContextPath()+"/resource/scripts/ym.js";
	js =new HashMap();
	js.put(ymJs,"1");
	
	out.write("<script type=\"text/javascript\" src=\""+ymJs+"\"></script>");
	request.setAttribute("tag-ym",js);
}
 

if (request.getAttribute(FormConstants.FORM_FIELDS) != null) {
	List<ExtField> fields = (List<ExtField>) request.getAttribute(FormConstants.FORM_FIELDS);
	fields.add(ext);
	request.setAttribute(FormConstants.FORM_FIELDS, fields);
}

String formItems=(String)request.getAttribute(FormConstants.FORM_ITEMS);
 
%>

<%!

/**
 * 分解月份为年月格式(两位年两位月)
 * 例如:24->0200 25->0201  23->0111
 * @param months
 * @return
 */
public static String parseMonth(int months){
	
	 return parseMonth(months,false);	
}

public static String parseMonth(int months,boolean showcn){
 
	StringBuffer result =new StringBuffer();
	
	int year =months /12;
	int month =months % 12;
	
	if(year <10)
		result.append("0");
	result.append(year);
	
	if(showcn){
		result.append("年");
	}
	
	if(month<10)
		result.append("0");
	result.append(month);
	
	if(showcn){
		result.append("月");
	}
	
	return result.toString();		
}

%>
 

<%
    //初始值处理
	String ym="";
	
	if(value!=null){
		int iValue =Integer.parseInt(value);
		
		//ym=parseMonth(iValue,false);
	}
	 
%>
<div id="${id}_div"></div>
<div id="_${id}_HIDDEN_div" style="display:none;"></div>
<script type="text/javascript">
<!--
	Ext.onReady(function(){
		var ${id}_REAL=new Ext.form.TextField({
			renderTo:'${id}_div',
			width:${width==null ? 160 : width},
			fieldLabel:"${label==null ? property : label}",
			//regex:/^(\d{2}0[0-9])|(\d{2}1[0-2])$/,
			invalidText:'请输入正常的年月数据,例如：0201',
			readOnly:${readOnly==null||readOnly==false ? false : true},
			hidden:${hidden==null||hidden==false ? false : true},
			maxLength:6,
			minLength:4 
			<%
			if(listeners==null){
				out.print(",listeners:{}");
			}else{
				out.print(",listeners:"+listeners);
			}
			%>
			<%
			if(required==null ||"false".equals(required)){
				out.print(",allowBlank:true");
			}else{
				out.print(",allowBlank:false");		
			}
			%>
			
		});

		 
		var ${id}=new Ext.form.TextField({
			id:"${id}",
			fieldLabel:"${label==null ? property : label}",
	        dataIndex:"${property}",
	        renderTo:"_${id}_HIDDEN_div"
		});
		
		//获取实际的值
		
		${id}.getRealValue =function(){
			return ymToMonths(${id}_REAL.getValue());
		};

		${id}.setValue =function(value){
	 
			if(!value){
				${id}.setRawValue(0);
				${id}_REAL.setValue('00年00月');
			 
				return;
			}
			${id}.setRawValue(value);
			${id}_REAL.setValue(monthsToym(value,${empty(showcn)||showcn eq "true" ? true : false}));
		}

		${id}.setRealValue =function(value){
			${id}.setValue(value);	
		}

		${id}_REAL.on('blur',function(t){
			${id}.setRawValue(ymToMonths(t.getValue(),${empty(showcn)||showcn eq "true" ? true : false}));
		});

		<%=formItems%>.push(${id});
		
	});
//-->
</script>
